package com.scholar.goods.controller;

import cn.hutool.json.JSONUtil;
import com.scholar.shop.common.BusinessException;
import com.scholar.shop.common.CommonRes;
import com.scholar.shop.common.EmBusinessError;
import com.scholar.shop.seller.login.LoginApi;
import com.scholar.shop.seller.login.SellerInfoInstance;
import com.scholar.shop.seller.login.SellerInfoOpenVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 卖家端登录controller
 * @author kimmy
 **/
@RestController
@RequestMapping("/seller/login")
public class SellerLoginController {

    private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(SellerLoginController.class);

    private static final String LOGIN_URL = "http://ecommerce-seller-api.ruyuan2020.com/seller/login/loginForStage";

    private static final String STATUS_FAIL = "fail";

    private static final String STATUS_SUCCESS = "success";

    @Autowired
    private LoginApi loginApi;

    /**
     * 登录
     * @param username 用户名
     * @param password 密码
     */
    @GetMapping
    public CommonRes<SellerInfoOpenVO> login(String username, String password) throws BusinessException {

        CommonRes<SellerInfoOpenVO> loginResult = loginApi.login(username, password);

        LOG.info("返回结果 {}", JSONUtil.toJsonPrettyStr(loginResult));

        if (STATUS_SUCCESS.equals(loginResult.getStatus())) {
            SellerInfoInstance.setSellerInfo(loginResult.getData());
        }

        return loginResult;
    }

    @GetMapping("/get")
    public SellerInfoOpenVO get() {
        return SellerInfoInstance.getInstance();
    }

}
